package com.chat.transmission.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author wei kunJie
 * @date 2024/3/22 20:54:36
 */
@Configuration
public class RedisConfig {
    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //创建RedisTemplate对象
        RedisTemplate<String, Object> stringObjectRedisTemplate =
                new RedisTemplate<String,Object>();
        //设置连接工厂
        stringObjectRedisTemplate.setConnectionFactory(redisConnectionFactory);
        //创建Json序列化工具
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer =
                new GenericJackson2JsonRedisSerializer();
        //设置key的序列化工具
        stringObjectRedisTemplate.setKeySerializer(RedisSerializer.string());
        //设置Value的序列化
        stringObjectRedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // 设置hash key的序列化器
        stringObjectRedisTemplate.setHashKeySerializer(stringRedisSerializer);
        // 设置hash value的序列化器
        stringObjectRedisTemplate.setHashValueSerializer(stringRedisSerializer);

        // 必须调用这个方法，初始化RedisTemplate
        stringObjectRedisTemplate.afterPropertiesSet();

        //返回
        return stringObjectRedisTemplate;
    }

    @Bean(name = "redisHashStringValue")
    public HashOperations<String, String, String> redisHashStringValue(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

    @Bean(name = "redisTemplateForObj")
    public RedisTemplate<String, Object> redisTemplateForObj(RedisConnectionFactory redisConnectionFactory) {
        //创建RedisTemplate对象
        RedisTemplate<String, Object> stringObjectRedisTemplate =
                new RedisTemplate<String,Object>();
        //设置连接工厂
        stringObjectRedisTemplate.setConnectionFactory(redisConnectionFactory);
        //创建Json序列化工具
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer =
                new GenericJackson2JsonRedisSerializer();
        //设置key的序列化工具
        stringObjectRedisTemplate.setKeySerializer(RedisSerializer.string());
        //设置Value的序列化
        stringObjectRedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // 设置hash key的序列化器
        stringObjectRedisTemplate.setHashKeySerializer(RedisSerializer.string());
        // 设置hash value的序列化器
        stringObjectRedisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        // 必须调用这个方法，初始化RedisTemplate
        stringObjectRedisTemplate.afterPropertiesSet();

        //返回
        return stringObjectRedisTemplate;
    }
    @Bean(name = "redisHashObjectValue")
    public HashOperations<String, String, Object> redisHashObjectValue(RedisTemplate<String, Object> redisTemplateForObj) {
        return redisTemplateForObj.opsForHash();
    }
}
